<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${daoPackage}.${className}Mapper">

	<!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="${entityPackage}.${className}" id="${classname}Map">
        <id property="${pk.attrname}" column="${pk.columnName}" />
<#list columns as column>
        <result property="${column.attrname}" column="${column.columnName}"/>
</#list>
    </resultMap>

    <sql id="where">
        <where>
<#list columns as column>
            <if test="params.${column.attrname} != null <#if column.attrType == 'String'>and params.${column.attrname} != ''</#if>">
                and ${column.columnName} =  ${r'#'}{params.${column.attrname}}
            </if>
</#list>
        </where>
    </sql>

    <sql id="order">
        <choose>
            <when test="params.orderby != null and params.orderby != ''">
                ORDER BY ${r'#'}{params.orderby}
            </when>
            <otherwise>
                ORDER BY ${pk.columnName} ASC
            </otherwise>
        </choose>
    </sql>


    <select id="list" resultMap="${classname}Map">
        select * from ${tableName}
        <include refid="where" />
        <include refid="order" />
    </select>

    <update id="update">
        update ${tableName}
        <set>
<#list columns as column>
             <if test="${column.attrname} != null <#if column.attrType == 'String'>and ${column.attrname} != ''</#if>">
                 `${column.columnName}` = ${r'#'}{${column.attrname}},
             </if>
</#list>
            update_time = now()
        </set>

        where ${pk.columnName} = ${r'#'}{${pk.attrname}}
    </update>

    <delete id="deleteBatch" parameterType="${pk.dataType}">
        delete from ${tableName}
        where ${pk.columnName} in
        <foreach item="${pk.attrname}" collection="${pk.attrName}s" open="(" separator="," close=")">
        ${r'#'}{${pk.attrname}}
        </foreach>
    </delete>

</mapper>
